Skip to content

Recover PDF text truncated by inline images using PyMuPDF fallback#2092

Open
Muhtasim-Munif-Fahim wants to merge 3 commits into
microsoft:mainfrom
Muhtasim-Munif-Fahim:codex/markitdown-csv-local-support
Open

Recover PDF text truncated by inline images using PyMuPDF fallback#2092
Muhtasim-Munif-Fahim wants to merge 3 commits into
microsoft:mainfrom
Muhtasim-Munif-Fahim:codex/markitdown-csv-local-support

Conversation

@Muhtasim-Munif-Fahim

Copy link
Copy Markdown

Fixes #1870.

Some PDFs with inline images cause pdfplumber/pdfminer to silently drop text that appears after the image. When the primary extraction path returns a much shorter body on image-bearing pages, this change optionally retries with PyMuPDF and prefers that output if it is substantially longer.

Also documents the optional pymupdf extra and adds a regression test that simulates the truncation path.

Copilot AI review requested due to automatic review settings June 8, 2026 13:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds an optional PyMuPDF-based text extraction fallback to improve PDF conversion when primary extractors (pdfplumber/pdfminer) appear to return truncated text, particularly around inline images.

Changes:

  • Introduces an optional PyMuPDF extraction path and heuristics to prefer it when primary output looks truncated.
  • Adds a regression test to validate choosing PyMuPDF when primary extraction misses post-image text.
  • Documents and packages PyMuPDF as an optional extra (and includes it in the all extra).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
packages/markitdown/tests/test_module_misc.py Adds a test that simulates truncation and asserts the PyMuPDF path is preferred.
packages/markitdown/src/markitdown/converters/_pdf_converter.py Adds PyMuPDF extraction helper and selection heuristics based on images + output length.
packages/markitdown/pyproject.toml Adds pymupdf optional dependency (extra + included in all).
packages/markitdown/README.md Documents how to install the optional PyMuPDF extra.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/markitdown/tests/test_module_misc.py Outdated
Comment thread packages/markitdown/src/markitdown/converters/_pdf_converter.py Outdated
Comment thread packages/markitdown/src/markitdown/converters/_pdf_converter.py Outdated
@Muhtasim-Munif-Fahim Muhtasim-Munif-Fahim force-pushed the codex/markitdown-csv-local-support branch from 0a99efc to 60c5aa8 Compare June 10, 2026 17:15
@Muhtasim-Munif-Fahim Muhtasim-Munif-Fahim force-pushed the codex/markitdown-csv-local-support branch from 60c5aa8 to 07d779c Compare June 11, 2026 08:16
@Muhtasim-Munif-Fahim

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

- Patch the module-level `fitz` name in the test instead of an attribute
  on it, since `fitz` is `None` when PyMuPDF isn't installed and
  `monkeypatch.setattr(None, "open", ...)` raises AttributeError.
- Avoid buffering the PDF twice: pass a zero-copy `getbuffer()` view to
  fitz.open() instead of `pdf_bytes.read()`, which duplicated the whole
  file in memory.
- Extract the magic thresholds (2048, 1.5, 200) into named module-level
  constants.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PDF text after an inline image (BI ... EI) is silently dropped from extraction

2 participants